<?php
/**
 * Created by andrei
 * Date: 9/13/12
 * Time: 2:34 PM
 */

class Page_Be_Products_Themes extends Page {

	/**
	 * @var Db_Products_Themes
	 */
	protected $tableObject;

	function __construct() {
		self::$TPL->appendJsFile('admin/themes');
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
			'saveAction'
		);
        $this->classTableObject = 'Db_Products_Themes';
        parent::__construct();
    }

    public function defaultAction() {
		if (perm('PRODUCTS_THEMES_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_TITLE, 'add'),'?action=addEdit');
		}
		$allThemes = Db_Products_Themes::getAllThemes();
        self::$TPL->assign('colors', $colors);
		self::$TPL->assign('allThemes', $allThemes);
		self::$TPL->setBodyTemplate('list_themes');
        return array(
            'type' => 'display'
        );
    }

	public function addEditAction() {
		perm('PRODUCTS_THEMES_EDIT', self::$PAGE['p_url']);
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_TITLE, 'edit') . ' &quot;' . t(T_THEMES_TITLE, $this->tableObject->objectArray->te_name).'&quot;', '', true);
		} else {
			$this->addBreadcrumb(t(T_FORM_TITLE, 'add'), '', true);
		}
		$types = array('theme', 'color');
		self::$TPL->assign('types', $types);
		self::$TPL->assign('editTheme', $this->tableObject->objectArray);
		$colors = Db_Products_Colors::getElementsForSelect('c_color_name');
		self::$TPL->assign('colors', $colors);
		self::$TPL->setBodyTemplate('add_edit_theme');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('PRODUCTS_THEMES_EDIT', self::$PAGE['p_url']);
		$this->tableObject->te_name = strtolower(post('te_name', 'str'));
		$this->tableObject->te_type = strtolower(post('te_type', 'str'));
        $this->tableObject->te_color_id = post('te_color_id', 'int');
		if ($this->tableObject->save()) {
			$this->message = t(T_FORM_MSG, 'success');
			return array(
				'type' => 'redirect',
				'query' => false
			);
		} else {
			$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
			$this->error = true;
			return array(
				'type' => 'redirect',
				'query' => false
			);
		}
	}

    public function deleteAction() {
		perm('PRODUCTS_THEMES_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
        if(!Db_Products_Themes::abstractDelete($this->idTableObject)) {
            $this->error = true;
            $this->message = t(T_FORM_MSG, 'unknown_error_occurred');
        }

        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

}